﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using devtm.Config;

namespace devtm.Configuration.Models
{

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, Inherited = true, AllowMultiple = true)]
    public sealed class RequireConfigurationSectionAttribute : CustomDesignerAttribute
    {
        // See the attribute guidelines at 
        //  http://go.microsoft.com/fwlink/?LinkId=85236
        readonly Type _typeSection;
        readonly string nameRequired;

        // This is a positional argument
        public RequireConfigurationSectionAttribute(Type typeSection)
            : base(ConfigConstants.Design.RequireConfigurationSectionParam)
        {
            this._typeSection = typeSection;
            DefaultSectionNameAttribute t = (DefaultSectionNameAttribute)Attribute.GetCustomAttribute(typeSection, typeof(DefaultSectionNameAttribute));
            if (t == null)
                throw new Exception("the section " + typeSection.Name + " don't have a DefaultSectionName Attribute. specify the nameRequired.");

            nameRequired = t.Name;

        }

        public RequireConfigurationSectionAttribute(Type typeSection, string nameRequired)
            : base(ConfigConstants.Design.RequireConfigurationSectionParam)
        {
            this._typeSection = typeSection;
            this.nameRequired = nameRequired;
        }

        /// <summary>
        /// Gets the type of the section required
        /// </summary>
        /// <value>The type section.</value>
        public Type TypeSection { get { return _typeSection; } }

        // Gets the name of the section required
        public string NameRequired { get { return nameRequired; } }

        public static void IsPresent(Object value)
        {

            RequireConfigurationSectionAttribute attr = null;

            if (value is Type)
                attr = GetRequiredAttributeType((Type)value);
            else
                attr = GetRequiredAttribute(value);

            if (attr != null)
                devtm.Helpers.ConfigurationHelper.GetSection<ConfigurationElement>(attr.NameRequired);
        }

        #region GetRequiredAttribute
        
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        private static  RequireConfigurationSectionAttribute GetRequiredAttributeType(Type type)
        {
            if (type != null)
                foreach (Attribute Attributeitem in type.GetCustomAttributes(typeof(RequireConfigurationSectionAttribute), true))
                    return (RequireConfigurationSectionAttribute)Attributeitem;
            return null;
        }

        private static RequireConfigurationSectionAttribute GetRequiredAttribute(object typeName)
        {
            return GetRequiredAttributeType(typeName.GetType());
        }

        #endregion
    
    }

}
